package com.keon.jianbao.ui.fragment;

import android.os.Bundle;
import android.view.View;

import com.keon.jianbao.R;
import com.keon.jianbao.adapter.CommentListAdapter;
import com.keon.jianbao.adapter.UpListAdapter;
import com.keon.jianbao.api.Api;
import com.keon.jianbao.api.SimpleHandler;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.base.BaseFragment;
import com.keon.jianbao.bean.CommentList;
import com.keon.jianbao.bean.Result;
import com.keon.jianbao.bean.UpList;
import com.keon.jianbao.ui.widget.NoScrollListView;

import org.apache.http.Header;
import org.json.JSONException;

import java.util.ArrayList;

import butterknife.Bind;

/**
 * Created by keon on 2015/9/9.
 */
public class TweetCommentGoodListFragment extends BaseFragment {

    public static int TYPE_COMMENT = 1;
    public static int TYPE_GOOD = 2;

    int state = 1;

    int size = 5;
    int object_id;
    int type;

    int mCurrentPage = 1;

    CommentListAdapter commentListAdapter;
    UpListAdapter upListAdapter;

    @Bind(R.id.listview)
    NoScrollListView nslvList;

    SimpleHandler getCommentHandler = new SimpleHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//            super.onSuccess(statusCode, headers, responseBody);
            try {
                Result result = new Result(responseBody);
                if (result.getStatus() > 0) {
                    if (type == TYPE_COMMENT) {
                        CommentList commentList = new CommentList(result.getContent());
                        commentListAdapter = new CommentListAdapter();
                        commentListAdapter.setData((ArrayList) commentList.getList());
                        nslvList.setAdapter(commentListAdapter);
                    } else if (type == TYPE_GOOD) {
                        UpList upList = new UpList(result.getContent());
                        upListAdapter = new UpListAdapter();
                        upListAdapter.setData((ArrayList) upList.getList());
                        nslvList.setAdapter(upListAdapter);
                    }

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    SimpleHandler nextCommentHandler = new SimpleHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            super.onSuccess(statusCode, headers, responseBody);
            try {
                Result result = new Result(responseBody);
                if (result.getStatus() > 0) {
                    if (type == TYPE_COMMENT) {
                        CommentList commentList = new CommentList(result.getContent());
                        commentListAdapter.addData((ArrayList) commentList.getList());
                        commentListAdapter.notifyDataSetChanged();

                        if (commentList.getList().size() == 0 || commentList.getList().size() < size) {
                            state = 2;
                        }
                    } else if (type == TYPE_GOOD) {
                        UpList upList = new UpList(result.getContent());
                        upListAdapter = new UpListAdapter();
                        upListAdapter.addData((ArrayList) upList.getList());
                        upListAdapter.notifyDataSetChanged();

                        if (upList.getList().size() == 0 || upList.getList().size() < size) {
                            state = 2;
                        }
                    }

                } else {
                    state = 2;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    @Override
    protected int getLayoutId() {

        return R.layout.fragment_tweet_comment;
    }

    @Override
    public void initView(View view) {

    }

    @Override
    public void initData() {
        getCommentHandler.setContext(this);
        nextCommentHandler.setContext(this);

        Bundle bundle = getArguments();
        object_id = bundle.getInt("id");
        type = bundle.getInt("type");

        getData();

    }

    @Override
    public void onClick(View v) {

    }

    public void next() {
        if (state != 1) {
            BaseActivity ba = (BaseActivity) getActivity();
            ba.showToast("没有更多数据");
            return;
        }
        mCurrentPage++;
        nextData();
    }

    public void refresh() {
        mCurrentPage = 1;
        getData();
    }

    private void getData() {
        if (type == TYPE_COMMENT) {
            Api.get_comment(object_id, 2, mCurrentPage, size, getCommentHandler);
        } else if (type == TYPE_GOOD) {
            Api.get_upvote(object_id, mCurrentPage, size, getCommentHandler);
        }
    }

    private void nextData() {
        if (type == TYPE_COMMENT) {
            Api.get_comment(object_id, 2, mCurrentPage, size, nextCommentHandler);
        } else if (type == TYPE_GOOD) {
            Api.get_upvote(object_id, mCurrentPage, size, nextCommentHandler);
        }
    }
}
